import { getUserChannels, getAllChannels } from '@/api/channels'
const state = {
  channelList: [],
  checkChannelsList: []
}
const mutations = {
  setUserChannels(state, payload) {
    state.channelList = payload
  },
  setCheckChannels(state, payload) {
    state.checkChannelsList = payload
  },
  addChannels(state, payload) {
    state.channelList.push(payload)
  },
  delChannels(state, payload) {
    state.channelList = state.channelList.filter((item) => item.id !== payload)
  }
}
const getters = {
  checkList(state) {
    return state.checkChannelsList.filter(
      (item) => !state.channelList.some((channels) => channels.id === item.id)
    )
  }
}
const actions = {
  async getUserChannels(context) {
    const res = await getUserChannels()
    context.commit('setUserChannels', res.data.channels)
  },
  async getAllChannelsList(context) {
    const res = await getAllChannels()
    context.commit('setCheckChannels', res.data.channels)
  }
}

export default {
  state,
  mutations,
  getters,
  actions,
  namespaced: true
}
